import datetime
import os
import sys
import time
from selenium import webdriver
from time import sleep,time
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.common.action_chains import ActionChains
class LikeShopTest():
    def __init__(self) -> None:

        driver_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(sys.executable)))),
                               'geckodriver.exe')
        service = FirefoxService(executable_path=driver_path)
        options = webdriver.FirefoxOptions()
        self.browser = webdriver.Firefox(service=service, options=options)

    def test_login(self):
        '''测试登录'''
        
        self.browser.get('http://likeshop.abc/admin/account/login.html')
        sleep(1)
        self.browser.find_element(By.NAME, 'account').send_keys('admin')
        self.browser.find_element(By.NAME, 'password').send_keys('root')
        self.browser.find_element(By.ID, 'login').click()
        sleep(2)
        
    def test_query_shop(self):
        '''测试查询商品'''
        
        #点击商品按钮
        self.browser.find_element(By.XPATH, '//*[@id="LAY-system-side-menu"]/li[1]/a').click()
        sleep(2)
        #定位ifnanme子框架
        iframe = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(iframe)
        sleep(2)
        #定位商品名称搜索框
        self.browser.find_element(By.ID,'keyword') .send_keys('文具')
        #定位查询按钮并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[1]/div/div[5]/div[3]/button[1]').click()
        sleep(2)
        #商品列表
        shop_list = self.browser.find_elements(By.XPATH, '/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')
        is_not_bug = True
        if len(shop_list) == 0:
            print('未查询到商品')
        else :
            for shop in shop_list:
            #  商品名称  
                shop_name = shop.find_element(By.XPATH,'./td[2]/div').text
                if '文具' not in shop_name:
                  print('有bug')
                  is_not_bug = False
                  break
        if is_not_bug:
            print('没有bug')
                
    def test_logout(self):
        '''测试退出登录'''
            
        #退出子框架
        self.browser.switch_to.default_content()
        sleep(2)
        #定位用户信息
        df = self.browser.find_element(By.XPATH, '//*[@id="LAY_app"]/div[1]/div[1]/ul[2]/li[3]/a/cite')
        #鼠标悬停
        ActionChains(self.browser).move_to_element(df).perform()
        sleep(2)      
        #点击退出
        self.browser.find_element(By.XPATH, '/html/body/div/div[1]/div[1]/ul[2]/li[3]/dl/dd[2]/a').click()
        sleep(2)
        
    def test_add_shop(self):
        '''发布商品测试'''
        #  #点击商品按钮
        # self.browser.find_element(By.XPATH, '//*[@id="LAY-system-side-menu"]/li[1]/a').click()
        # sleep(2)
        # #定位ifnanme子框架
        # iframe = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        # self.browser.switch_to.frame(iframe)
        # sleep(2)
        #定位发布商品按钮并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[2]/div[1]/button[1]').click()
        sleep(2)
        #定位商品发布子框架
        iframe_add = self.browser.find_element(By.XPATH,'//*[@id="layui-layer-iframe1"]')
        self.browser.switch_to.frame(iframe_add)
        sleep(2)
        #定位商品名称输入框并输入商品名称
        now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        self.browser.find_element(By.NAME,'name').send_keys('测试商品2'+now)
        sleep(1)
        #定位第一个分类的选项框并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/div/input').click()
        sleep(1)
        #定位第一个分类的下拉项并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/dl/dd[2]').click()
        sleep(1)
        #定位第二个分类的选项框并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/div/input').click()
        sleep(1)
        #定位第二个分类的下拉项并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/dl/dd[2]').click()
        sleep(1)
        #定位第三个分类的选项框并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/div/input').click()
        sleep(1)
        #定位第三个分类的下拉项并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/dl/dd[2]').click()
        sleep(1)
        
        #定位商品主图的添加图片按钮并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[5]/div/div[2]/a').click()
        sleep(1)
        self.test_upload_img('E:\\project\\1.png','//*[@id="layui-layer-iframe2"]')
        #返回商品子框架
        self.browser.switch_to.parent_frame()
        #切换到发布商品子框架
        self.browser.switch_to.frame(iframe_add)
        sleep(2)
        #定位商品轮播图的添加图片按钮并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[6]/div/div/a').click()
        sleep(1)
        self.test_upload_img('E:\\project\\1.png','//*[@id="layui-layer-iframe3"]')
        #返回商品子框架
        self.browser.switch_to.parent_frame()
        #切换到发布商品子框架
        self.browser.switch_to.frame(iframe_add)
        sleep(2)
        #切换到价格设置标签页
        self.browser.find_element(By.XPATH,'/html/body/div/ul/li[2]').click()
        sleep(2)
        #定位市场价输入框并输入市场价
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[2]/input').send_keys('100')
        sleep(1)
        #定位价格输入框并输入价格
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[3]/input').send_keys('100')
        sleep(1)
        #定位成本输入框并输入成本价
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[4]/input').send_keys('100')
        sleep(1)
        #定位库存输入框并输入库存
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[5]/input').send_keys('100')
        sleep(1)
        
        #切换到销售设置标签页
        self.browser.find_element(By.XPATH,'/html/body/div/ul/li[4]').click()
        sleep(2)
        #定位配送方式选项框并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[4]/div[6]/div/div[1]/span').click()
        sleep(1)
        #返回到商品子框架
        self.browser.switch_to.parent_frame()
        sleep(1)
        #点击保存按钮
        self.browser.find_element(By.CLASS_NAME,'layui-layer-btn0').click()
        sleep(2)
        
        
    def test_sj_shop(self):
        '''上架商品'''
        #  #点击商品按钮
        # self.browser.find_element(By.XPATH, '//*[@id="LAY-system-side-menu"]/li[1]/a/cite').click()
        # sleep(2)
        #定位ifnanme子框架
        # iframe = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        # self.browser.switch_to.frame(iframe)
        # sleep(2)
        # #定位仓库中商品的切换标签页
        self.browser.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/ul/li[2]').click()
        sleep(2)
        #接收商品列表
        goods_list = self.browser.find_elements(By.XPATH,'/html/body/div[1]/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr')
        sleep(2)
        if len(goods_list) > 0:
            #定位第一个商品的上架按钮并点击
            goods_list[0].find_element(By.XPATH,'/html/body/div[1]/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr[1]/td/div/a[2]').click()
            sleep(2)
            #定位确认上架按钮并点击
            self.browser.find_element(By.CLASS_NAME,'layui-layer-btn0').click()
            sleep(2)
        else:
            print('仓库中暂无商品')
        
     
    def test_edit_shop(self):  
        """编辑商品"""  
         #点击商品按钮
        # self.browser.find_element(By.XPATH, '//*[@id="LAY-system-side-menu"]/li[1]/a/cite').click()
        # sleep(2)
        # #定位ifnanme子框架
        # iframe = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        # self.browser.switch_to.frame(iframe)
        # sleep(2)
        
        #定位销售中商品的切换标签页
        self.browser.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/ul/li[1]').click()
        sleep(2)
        #定位编辑商品子框架
        iframe_add = self.browser.find_element(By.XPATH,'//*[@id="layui-layer-iframe3"]')
        self.browser.switch_to.frame(iframe_add)
        sleep(2)
        #定位商品名称输入框删除原来的名称并输入新的商品名称
        name_input = self.browser.find_element(By.NAME,'name')
        name_input.clear()  # 清空先前的名称
        now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        self.browser.find_element(By.NAME,'name').send_keys('测试商品2'+now)
        sleep(1)
        #定位第一个分类的选项框并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/div/input').click()
        sleep(1)
        #定位第一个分类的下拉项并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/dl/dd[2]').click()
        sleep(1)
        #定位第二个分类的选项框并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/div/input').click()
        sleep(1)
        #定位第二个分类的下拉项并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/dl/dd[6]').click()
        sleep(1)
         #定位第三个分类的选项框并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/div/input').click()
        sleep(1)
        #定位第三个分类的下拉项并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/dl/dd[2]').click()
        sleep(1)
        #切换到价格设置标签页
        self.browser.find_element(By.XPATH,'/html/body/div/ul/li[2]').click()
        sleep(2)
        #定位市场价输入框并输入市场价
        market_price_input = self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[2]/input')
        market_price_input.clear()  # 清空先前的价格
        market_price_input.send_keys('2')
        sleep(1)
        #定位价格输入框并输入价格
        #self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[3]/input').send_keys('3')
        market_price_input = self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[3]/input')
        market_price_input.clear()  # 清空先前的价格
        market_price_input.send_keys('2')
        sleep(1)
        
        #定位成本输入框并输入成本价
        market_price_input = self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[4]/input')
        market_price_input.clear()  # 清空先前的价格
        market_price_input.send_keys('2')
        sleep(1)
        #定位库存输入框并输入库存
        market_price_input = self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[5]/input')
        market_price_input.clear()  # 清空先前的价格
        market_price_input.send_keys('2')
        sleep(1)
         #切换到销售设置标签页
        self.browser.find_element(By.XPATH,'/html/body/div/ul/li[4]').click()
        sleep(2)
        #定位配送方式选项框并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[4]/div[6]/div/div[2]/i').click()
        sleep(1)
        # #返回到商品子框架
        self.browser.switch_to.parent_frame()
        sleep(1)
        #点击保存按钮
        self.browser.find_element(By.CLASS_NAME,'layui-layer-btn0').click()
        sleep(2)
        
        
        
        

        # #返回商品子框架
        # self.browser.switch_to.parent_frame()
        # #定位上传图片的子框架并进入子框架
        # iframe_img = self.browser.find_element(By.XPATH,'//*[@id="layui-layer-iframe2"]')
        # self.browser.switch_to.frame(iframe_img)
        # sleep(2)
        # #定位图片上传输入框并上传图片
        # self.browser.find_element(By.XPATH,'/html/body/div/div[2]/div[1]/div[1]/input').send_keys('E:\\project\\1.png')
        # sleep(4)
        # #选择第一张图片
        # self.browser.find_element(By.XPATH,'/html/body/div/div[2]/div[1]/div[2]/div/ul/li[1]/div/img').click()
        # sleep(2)
        # #点击使用图片按钮
        # self.browser.find_element(By.XPATH,'//*[@id="used"]').click()
        # sleep(2)
    
    def test_upload_img(self,img_path,xpath_path):
        '''测试上传商品图片'''
        
         #返回商品子框架
        self.browser.switch_to.parent_frame()
        #定位上传图片的子框架并进入子框架
        iframe_img = self.browser.find_element(By.XPATH,xpath_path)
        self.browser.switch_to.frame(iframe_img)
        sleep(2)
        #定位图片上传输入框并上传图片
        self.browser.find_element(By.XPATH,'/html/body/div/div[2]/div[1]/div[1]/input').send_keys(img_path)
        sleep(4)
        #选择第一张图片
        self.browser.find_element(By.XPATH,'/html/body/div/div[2]/div[1]/div[2]/div/ul/li[1]/div/img').click()
        sleep(2)
        #点击使用图片按钮
        self.browser.find_element(By.XPATH,'//*[@id="used"]').click()
        sleep(2)
        
        

if __name__=='__main__':
    lk = LikeShopTest()
    lk.test_login()
    lk.test_query_shop()
    lk.test_add_shop()
    lk.test_sj_shop()
    lk.test_edit_shop()
    lk.test_logout()
